package com.wh.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.vo.AjaxResult;
import com.wh.org.dto.EmployeeDto;
import com.wh.org.pojo.Employee;
import com.wh.org.service.IEmployeeService;
import com.wh.system.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/operationLog")
public class OperationLogController {

    @Autowired
    private IEmployeeService service;

    @PreAuthorize(sn = "operationLog/addOrUpd",name = "添加或修改操作日志")
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Employee employee ){
        try {
            if (employee.getId()!=null){
                Long id = employee.getId();
                System.err.println(id);
                service.updState(id);
                return AjaxResult.me();
            }else{
                service.add(employee);
                return AjaxResult.me();
            }
        }catch (Exception ex){
            return AjaxResult.me().setSuccess(false).setMessage("出现异常");
        }
    }

//    @PreAuthorize(sn = "operationLog/updState",name = "添加或修改操作日志")
    @PutMapping("/{id}")
    public AjaxResult updState(@PathVariable("id") Long id){
        service.updState(id);
        System.out.println(id);
        return AjaxResult.me();
    }


    @PreAuthorize(sn = "operationLog/del",name = "根据id删除操作日志")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        service.del(id);
        return AjaxResult.me();
    }

//    @PreAuthorize(sn = "operationLog/addOrUpd",name = "添加或修改操作日志")
    @GetMapping
    public List<Employee> findAll(){
        return service.findAll();
    }

    @PreAuthorize(sn = "operationLog/patchDel",name = "批量删除操作日志")
    @PatchMapping
    public AjaxResult patchDel(Long[] ids){
        service.patchDel(ids);
        return AjaxResult.me();
    }

    @PreAuthorize(sn = "operationLog/findById",name = "根据id查找操作日志")
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return service.findById(id);
    }


    @PreAuthorize(sn = "operationLog/fingByPage",name = "分页查找操作日志")
    @PostMapping
    public IPage<Employee> fingByPage(@RequestBody EmployeeDto employeeDto){
        return service.findByPage(employeeDto);
    }


    /**
     * 根据店铺id，查询店铺员工
     * @param shopId
     * @return 店铺员工们
     */
    @GetMapping("list/{shopId}")
    public List<Employee> listByShopId(@PathVariable Long shopId){
        return service.listByShopId(shopId);
    }
}
